<template>
  <!-- 权限管理 -->
  <div class="box">
    <!-- 搜索 新增 -->
    <div class="searchFor">
      <el-form :inline="true" :model="formInline">
        <el-input
          v-model="formInline.user"
          placeholder="根据用户名搜索"
          clearable
        ></el-input>
        <el-button size="small" @click="submit">清空</el-button>
        <el-button
          size="small"
          type="primary"
          @click="onSubmit(formInline.user)"
          >搜索
        </el-button>
      </el-form>
      <el-button
        icon="el-icon-edit"
        size="small"
        type="success"
        @click="openDrawer('add')"
      >
        新增权限组
      </el-button>
    </div>
    <!-- 搜索 新增 -->
    <!-- 记录 -->
    <div class="record">
      <i class="el-icon-info"></i>
      <span>共 {{ total }} 条记录</span>
    </div>
    <!-- 记录 -->
    <!-- table 表格数据区域 -->
    <el-table
      ref="multipleTable"
      :data="list"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
      :default-sort="{ prop: 'create_date', order: 'descending' }"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column
        prop="title"
        label="用户名"
        min-width="80"
        align="center"
      >
      </el-table-column>
      <el-table-column label="日期" min-width="80" prop="create_date" sortable>
      </el-table-column>
      <el-table-column prop="address" label="操作" width="120" align="center">
        <template #default="{ row }">
          <div class="actions">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              plain
              @click="openDrawer('edit', row.id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              plain
              @click="del(row.id)"
            ></el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- table 表格数据区域 -->

    <!-- 分页组件区域 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryObj.page"
        :page-size="queryObj.pagesize"
        layout=" prev, pager, next, sizes, jumper"
        :page-sizes="[10, 20, 30, 50]"
        background
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 分页组件区域 -->

    <!-- 弹窗组件 -->
    <UsersAdd
      ref="usersAdd"
      text="创建用户"
      :dialogVisible.sync="dialogVisible"
      :id="currentId"
      :pageTitle="pageTitle"
      :PermissionGroupsList="list"
      :ruleInline="rules"
    ></UsersAdd>
    <!-- <el-dialog
      :title="drawerTitle"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
      width="50%"
    >
      <span slot="footer" class="dialog-footer">
        <div class="form">
          <el-form :rules="rules" :model="form" ref="form" label-width="100px">
            <el-form-item label="用户名：" prop="title">
              <el-input placeholder="请输入用户名" v-model="form.title" />
            </el-form-item>
            <el-form-item label="权限分配：" prop="title">
              <el-tree
                :data="data"
                show-checkbox
                node-key="id"
                :default-expanded-keys="[1, 3]"
                :props="defaultProps"
              >
              </el-tree>
            </el-form-item>
          </el-form>
          <div class="button">
            <el-button @click="handleClose">取 消</el-button>
            <el-button type="primary" @click="increaseAdd">确 定</el-button>
          </div>
        </div>
      </span>
    </el-dialog> -->
    <!-- 弹窗组件 -->
  </div>
</template>

<script>
import { list, remove } from '@/api/base/permissions.js'
import UsersAdd from '../components/permissions-add.vue'

export default {
  components: {
    UsersAdd
  },
  data () {
    return {
      list: [],
      formInline: {
        user: ''
      },
      queryObj: {
        page: 1, // 控制当前页
        pagesize: 10 // 控制每一页有几条数据
      },
      total: 0, // 共多少条
      pageTitle: 0,
      // 树形控件
      data: [
        {
          id: 1,
          label: '一级 1',
          children: [
            {
              id: 4,
              label: '二级 1-1',
              children: [
                {
                  id: 9,
                  label: '三级 1-1-1'
                },
                {
                  id: 10,
                  label: '三级 1-1-2'
                }
              ]
            }
          ]
        }
      ],
      // 树形控件
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      dialogVisible: false, // 打开添加
      drawerType: 'add', // 打开添加
      currentId: null,
      // form表单
      form: {
        id: 0,
        create_date: '',
        title: '',
        permissions: []
      },
      rules: {
        title: [{ required: true, message: '请输入用户名', trigger: 'blur' }]
      }
    }
  },

  created () {
    this.initTable()
  },

  methods: {
    // 多选
    toggleSelection (rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    // 排序
    formatter (row, column) {
      return row.address
    },
    // 清空搜索
    submit () {
      this.formInline.user = ''
      this.initTable()
    },
    // 搜索
    onSubmit (user) {
      // console.log(this.list.title)
      // console.log(user)
      this.list = this.list.filter(item => {
        console.log(item.title)
        return item.title === user
      })
    },
    // 获取table数据
    async initTable () {
      const { data } = await list()
      console.log(data)

      this.list = data.list
      this.total = data.counts
      console.log(this.list)
    },
    // 分页组件
    // pageSize 改变时会触发
    handleSizeChange (pageSize) {
      console.log(`每页 ${pageSize} 条`)
      this.queryObj.pagesize = pageSize
      // this.queryObj.page = 1
      this.initTable()
    },

    // current当前页改变时会触发
    handleCurrentChange (page) {
      console.log(`当前页: ${page}`)
      // 改变参数对象中的当前页
      this.queryObj.page = page
      this.initTable()
    },

    // 点击删除图标删除对应的数据
    async del (id) {
      console.log(id)
      try {
        await remove(id)
        this.$message.success('删除成功')
        this.initTable()
      } catch (error) {
        this.$message.error('删除失败')
      }
    },
    // 打开添加组件
    async openDrawer (type, id) {
      this.drawerType = type

      // 添加功能
      if (type === 'add') {
        // 用户详情
        this.dialogVisible = true // 显示弹层
        this.currentId = id
      }
      this.dialogVisible = true
    },
    // 添加组件关闭之前触发
    handleClose () {
      // 清空表单
      this.form = ''

      // 重置表单
      if (this.drawerType !== 'preview') {
        // red edit 调用 resetFields 在此处的作用: 重置校验状态
        this.$refs.form.resetFields()
      }

      this.dialogVisible = false
    }
  }
}
</script>

<style scoped lang="less">
.box {
  padding: 20px;
  border: 1px solid #ebeef5;
  background-color: #fff;
  color: #303133;
  margin: 20px;
  .searchFor {
    display: flex;
    justify-content: space-between;
    height: 32px;
    .el-form {
      display: flex;
      .el-input {
        width: 200px;
        font-size: 13px;
      }
      .el-button {
        margin-left: 15px;
      }
    }
  }
  .record {
    width: 100%;
    margin: 20px 0;
    background-color: #f4f4f5;
    color: #909399;
    padding: 8px 16px;
    span {
      padding: 0 8px;
      font-size: 13px;
      line-height: 18px;
    }
  }
  .el-table {
    width: 100%;
    ::v-deep .el-table__header-wrapper {
      border-bottom: 2px solid #e8e8e8;
      ::v-deep .has-gutter {
        ::v-deep .el-table_1_column_2 {
          text-align: center;
        }
        ::v-deep .el-table_1_column_4 {
          text-align: center;
        }
      }
      .hidden-columns {
        background-color: #fafafa;
      }
    }
  }
  .block {
    display: flex;
    justify-content: flex-end;
    margin-top: 20px;
    ::v-deep .el-pagination__jump {
      margin-left: 0;
    }
  }
  .dialog-footer {
    padding: 30px 20px;
    color: #606266;
    font-size: 14px;
    word-break: break-all;
    .form {
      width: 400px;
      margin-left: 120px;
      .el-tree {
        margin-top: 10px;
      }
    }
  }
}
</style>
